//基本提交
function commit(postType, dataType, postData, url, successFunction, failFunction) {
    $.ajax({
        type: postType,
        data: postData,
        url: url,
        success: successFunction,
        error: failFunction
    });
}

//勾选时特殊处理 json数据格式
function postJsonCommit(postData, url, successFunction, failFunction) {
    $.ajax({
        type: "POST",
        contentType : "application/json",
        data: postData,
        dataType: "json",
        url: url,
        success: successFunction,
        error: failFunction
    });
}

//表单形式提交
function postCommit(postData, url, successFunction, failFunction) {
    return commit("POST", "html", postData, url, successFunction, failFunction);
}

function putCommit(postData, url, successFunction, failFunction) {
    return commit("PUT", "html", postData, url, successFunction, failFunction);
}

function getCommit(url, successFunction, failFunction) {
    // alert("GET");
    return commit("GET", "html", null, url, successFunction, failFunction);
}

function deleteCommit(url, successFunction, failFunction) {
    return commit("DELETE", "html", null, url, successFunction, failFunction);
}
/*************************************************基本提交 end ***********************************************************/
/*************************************************基本提示 begin ***********************************************************/

//调用失败提示
function errorInfo(resultData) {
    // alert("error:"+resultData.responseText);
    layer.msg("操作失败！" + resultData.responseText, {
        icon: 5,//提示的样式
        time: 3000, //2秒关闭（如果不配置，默认是3秒）//设置后不需要自己写定时关闭了，单位是毫秒
    });
}

//警告提示
function msgInfo(msg) {
    // alert("error:"+resultData.responseText);
    layer.msg("操作失败！" + msg, {
        icon: 5,//提示的样式
        time: 3000, //2秒关闭（如果不配置，默认是3秒）//设置后不需要自己写定时关闭了，单位是毫秒
    });
}
//成功提示
function successInfo() {
    layer.msg("操作成功", {
        icon: 6,//提示的样式
        time: 2000, //2秒关闭（如果不配置，默认是3秒）//设置后不需要自己写定时关闭了，单位是毫秒
    });
}

//提示处理
function confirm(msg, invokeFunction) {
    // layer.confirm(msg, {
    //     btn: ['是', '否'] //按钮
    // }, function () {
    //     invokeFunction;
    // }, function () {
    //
    // });
    layer.confirm(msg, {icon: 3, title:'提示'}, function(index){
        layer.close(index);
        invokeFunction();
    });
    return false;
}

function dealResult(resultData){
    var code = resultData.code;
    if(code == 0){
        successInfo();
        return true;
    }else{
        msgInfo(resultData.msg);
        return "error";
    }
}

/*************************************************基本提示 end ***********************************************************/
/*************************************************默认提交动作 begin ***********************************************************/
//默认新增提交
function defaultAddRecord(url) {
    var modifyForm = $("#form_add").serialize();
    postCommit(modifyForm, url, function(resultData){
        $('#modal_add').modal('hide');
        if(dealResult(resultData) == false){
            return;
        }
        //异步刷新
        asyncRefreshDiv($("#list_table"), "#list_table");
        //清空form
        clearForm($("#form_add"));
    }, errorInfo);
}

//默认删除记录
function defaultDelRecord(data, requestUrl) {
    var tdList = $(data).parent().parent().children();
    var id = tdList.eq(0).text();
    var url = requestUrl + id;
    var msg = "是否删除该记录？";
    confirm(msg, function(){
        deleteCommit(url, function (resultData) {
            asyncRefreshDiv($("#list_table"), "#list_table");
        }, errorInfo);
    });
}

//默认修改记录
function defaultUpdateRecord(url) {
    var modifyForm = $("#form_modify").serialize();
    putCommit(modifyForm, url, function (resultData) {
        if(dealResult(resultData) == false){
            return;
        }
        asyncRefreshDiv($("#list_table"), "#list_table");
        $('#modal_modify').modal('hide');
    }, errorInfo);
}
//默认修改记录（Get）
function defaultUpdateRecordByGet(url) {
    getCommit(url,function (resultData) {
        asyncRefreshDiv($("#list_table"), " #list_table");
        $('#modal_modify').modal('hide');
    }, errorInfo);
}

//数据提交方式为json(用户授权，角色授权类似场景使用)
function defaultSaveRecord(postData, url) {
    postJsonCommit(postData, url, function (resultData) {
        if(dealResult(resultData) == false){
            return;
        }
        asyncRefreshDiv($("#list_table"), "#list_table");
        $('#modal_modify').modal('hide');
    }, errorInfo);
}

//默认记录翻页
function defaultPageRecord(pageSize, nowPageNo, requestUrl) {
    // var pageSize = "${listResult.pageSize}";
    var pageNo = 1;
    if(nowPageNo){
        pageNo = nowPageNo;
    }
    var requestData = "?pageNo=" + pageNo + "&pageSize=" + pageSize;
    //遍历form表单的查询条件
    var fromSerialize = $("#queryForm").serializeArray();
    $.each(fromSerialize, function() {
        if(this.value){
            requestData = requestData + "&" + this.name + "=" + this.value;
        }
    });
    var url = requestUrl + requestData;
    //局部刷新
    asyncRefreshURL(url, $("#list_table"), " #list_table");
}

//默认修改模态窗弹出
function defaultSkipModifyModal(data, requestUrl) {
    var tdList = $(data).parent().parent().children();
    var id = tdList.eq(0).text();
    var url = requestUrl + id;
    getCommit(url,function (resultData) {
        asyncRefresh(resultData, $("#info_modify"));
        $('#modal_modify').modal('show');
    }, errorInfo);
}

/*************************************************默认提交动作 end ***********************************************************/

function dealSuccessFunction(resultData, dealFunction){
    // 1：勾号
    // 2：X号
    // 3：？号
    // 4：锁
    // 5：哭脸
    // 6：笑脸
    // 7：！号
    alert(resultData);
    var result = JSON.parse(resultData);
    if(result.code != 0){
        // layer.msg('删除成功', {
        //     btn: ['明白了', '知道了']
        //     ,yes: function(index, layero){
        //         layer.msg("按钮1回调,参数是:"+index)
        //     }
        //     ,btn2: function(index, layero){
        //         //按钮【按钮二】的回调
        //         layer.msg("按钮2回调,参数是:"+index)
        //         return false //开启该代码可禁止点击该按钮关闭
        //     }
        // });

        layer.msg(result.msg, {
            icon: 1,//提示的样式
            time: 4000, //2秒关闭（如果不配置，默认是3秒）//设置后不需要自己写定时关闭了，单位是毫秒
            end:function(){
                dealFunction
            }
        });
        // layer.msg('删除成功', {icon: 1});
        // dealFunction
    }else{
        layer.msg(result.msg, {icon: 5,
            time: 6*1000
            // ,btn: ['关闭']
        });
    }
}







/**********************************************************************************************************************/
//异步刷新
function asyncRefreshDiv(divId, divIdStr) {
    $(divId).load(location.href+" " + divIdStr);
}

//异步刷新(指定url)
function asyncRefreshURL(url, divId, divIdStr) {
    $(divId).load(url + divIdStr);
}

//网页重新请求
function asyncRefresh(resultData, divId) {
    $(divId).html(resultData);
}


// 复选框全选/取消全选
function selectAllBox(data) {
    // alert($(data).attr("checked"));
    if($(data).attr("checked")=='checked'){
        //取消全选
        $("input:checkbox").each(function (index, target) {
            $(target).attr("checked", false);
        });
    }else{
        //全选
        $("input:checkbox").each(function (index, target) {
            $(target).attr("checked", true);
        });
    }
}

//form清空
function clearForm(form) {
    $(":input",form)
        .not(":button,:submit,:reset,:hidden")   //将myform表单中input元素type为button、submit、reset、hidden排除
        .val("")//将input元素的value设为空值
        .removeAttr("checked")
        .removeAttr("checked"); // 如果任何radio/checkbox/select inputs有checked or selected 属性，将其移除

}